package com.resource.manager.ui;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;

public class FileToClipboard {

    public static void copyFileToClipboard(File file) {
        // 获取系统剪贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

        // 创建一个Transferable对象，用于封装要传输的数据
        Transferable transferable = new Transferable() {
            @Override
            public DataFlavor[] getTransferDataFlavors() {
                return new DataFlavor[]{DataFlavor.javaFileListFlavor};
            }

            @Override
            public boolean isDataFlavorSupported(DataFlavor flavor) {
                return flavor.equals(DataFlavor.javaFileListFlavor);
            }

            @Override
            public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
                if (flavor.equals(DataFlavor.javaFileListFlavor)) {
                    return java.util.Collections.singletonList(file);
                } else {
                    throw new UnsupportedFlavorException(flavor);
                }
            }
        };

        // 将文件设置为剪贴板的内容
        clipboard.setContents(transferable, null);
    }

}
